package com.showde.shoppingmall.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityGroup;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.adapter.RecommendAdapter;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.ProductFetchOnCriteriaLogic;
import com.showde.shoppingmall.business.logic.ProductSearchLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.domain.Product;
import com.showde.shoppingmall.netentity.ProductSearchEntity.FETCH_TYPE;
import com.showde.shoppingmall.utils.ActivityDispatch;

@SuppressWarnings("deprecation")
public class RecommendActivity extends BaseActivity {

	private static final String TAG = "RecommendActivity";

	private static final int MSG_REFRESHLISTDATA = 0x10001;

	private ProductSearchLogic productSearchLogic;

	private RecommendAdapter recommendAdapter;
	private ListView productList;
	private List<Product> products;

	private UICallbackInterface callbackInterface;

	private LinearLayout layoutZX;// 最新
	private LinearLayout layoutJG; // 价格
	private LinearLayout layoutXL; // 销量

	private LinearLayout rcLayout; // 顶部按钮栏

	private ImageView zxImageView;// img_tnew
	private ImageView xlImageView;// img_salesvolume
	private ImageView jgImageView;// img_tprice

	ImageLoader imageLoader;

	private String recommendType = null;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_REFRESHLISTDATA:
				fillList();
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.ll_zx:
			zxImageView.setVisibility(View.VISIBLE);
			xlImageView.setVisibility(View.INVISIBLE);
			jgImageView.setVisibility(View.INVISIBLE);

			ProductFetchOnCriteriaLogic.fetchSummary(this,
					FETCH_TYPE.ORDERBY_ZX, callbackInterface);
			break;
		case R.id.ll_jg:

			zxImageView.setVisibility(View.INVISIBLE);
			xlImageView.setVisibility(View.INVISIBLE);
			jgImageView.setVisibility(View.VISIBLE);

			ProductFetchOnCriteriaLogic.fetchSummary(this,
					FETCH_TYPE.ORDERBY_JG, callbackInterface);
			break;
		case R.id.ll_xl:

			zxImageView.setVisibility(View.INVISIBLE);
			xlImageView.setVisibility(View.VISIBLE);
			jgImageView.setVisibility(View.INVISIBLE);

			ProductFetchOnCriteriaLogic.fetchSummary(this,
					FETCH_TYPE.ORDERBY_XL, callbackInterface);
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		fillList();
		if (recommendType != null) {
			rcLayout.setVisibility(View.GONE);
			FETCH_TYPE type = null;
			if ("tmh".equals(recommendType)) {
				type = FETCH_TYPE.ORDERBY_JG;
			} else if ("phb".equals(recommendType)) {
				type = FETCH_TYPE.ORDERBY_XL;
			}
			ProductFetchOnCriteriaLogic.fetchSummary(this, type,
					callbackInterface);
		} else {
			ProductFetchOnCriteriaLogic.fetchSummary(this,
					FETCH_TYPE.ORDERBY_JG, callbackInterface);
		}
	}

	@Override
	protected void initParam() {

		ActivityGroup activityGroup = ActivityDispatch
				.getActivityGroup(groupTag);
		if (activityGroup == null) {
			groupTag = ActivityDispatch.GROUP_RECOMMEND;
		}

		imageLoader = new ImageLoader(this);

		callbackInterface = new UICallbackInterface() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(CallbackData data) {
				if (data != null) {
					products = (List<Product>) data.data;
					handler.sendEmptyMessage(MSG_REFRESHLISTDATA);
				}
			}

			@Override
			public void onError(String errorMsg) {
				Log.d(TAG, "responseData:" + errorMsg);
			}
		};

		recommendType = intent
				.getStringExtra(ActivityDispatch.PRODUCT_RECOMMEND);
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_recommend);
		productList = (ListView) findViewById(R.id.lst_product);

		productList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Product product = products.get(position);
				Bundle bundle = new Bundle();
				bundle.putString(ActivityDispatch.PRODUCT_ID,
						product.getProductID());
				ActivityDispatch.openNewActivity(RecommendActivity.this,
						ProductDetailsActivity.class,
						ActivityDispatch.getActivityGroup(groupTag),
						"ShoppingcarActivity", bundle, groupTag);
			}
		});

		layoutZX = (LinearLayout) findViewById(R.id.ll_zx);
		layoutJG = (LinearLayout) findViewById(R.id.ll_jg);
		layoutXL = (LinearLayout) findViewById(R.id.ll_xl);

		layoutZX.setOnClickListener(this);
		layoutJG.setOnClickListener(this);
		layoutXL.setOnClickListener(this);

		zxImageView = (ImageView) findViewById(R.id.img_tnew);
		xlImageView = (ImageView) findViewById(R.id.img_salesvolume);
		jgImageView = (ImageView) findViewById(R.id.img_tprice);

		rcLayout = (LinearLayout) findViewById(R.id.ll_rc);
	}

	private void fillList() {
		if (products == null) {
			products = new ArrayList<Product>();
		}
		recommendAdapter = (RecommendAdapter) productList.getAdapter();
		if (recommendAdapter == null) {
			recommendAdapter = new RecommendAdapter(this, products, imageLoader);
			productList.setAdapter(recommendAdapter);
		} else {
			recommendAdapter.resetData(products);
		}
		recommendAdapter.notifyDataSetChanged();
	}
}
